"""
工具6：通知发送工具
功能：发送邮件/短信/站内通知
"""
import json
import logging
from langchain.tools import BaseTool

logger = logging.getLogger(__name__)


class SendNotificationTool(BaseTool):
    """通知发送工具"""
    
    name: str = "send_notification_tool"
    description: str = """
        发送通知（邮件/短信/站内信）。
        
        输入参数（JSON字符串）：
        {
            "receiver_id": 123,
            "content": "您好，邀请您参加面试...",
            "channel": "email"  // email/sms/internal
        }
        """
    
    def _run(self, tool_input: str) -> str:
        try:
            # 解析输入
            if isinstance(tool_input, str):
                input_dict = json.loads(tool_input)
            elif isinstance(tool_input, dict):
                input_dict = tool_input
            else:
                return json.dumps({"success": False, "error": f"不支持的输入类型: {type(tool_input)}"}, ensure_ascii=False)
            
            receiver_id = input_dict.get('receiver_id')
            content = input_dict.get('content')
            channel = input_dict.get('channel', 'internal')
            
            if not receiver_id or not content:
                return json.dumps({"success": False, "error": "必须提供receiver_id和content"}, ensure_ascii=False)
            
            # 简化实现：记录日志
            logger.info(f"发送通知: receiver={receiver_id}, channel={channel}, content={content[:50]}")
            
            # TODO: 实际实现时调用现有的邮件/短信服务

            
            return json.dumps({
                "success": True,
                "data": {"notification_id": "notif_123"},
                "message": "通知发送成功"
            }, ensure_ascii=False)
            
        except json.JSONDecodeError as e:
            logger.error(f"JSON解析失败: {str(e)}")
            return json.dumps({"success": False, "error": f"JSON解析失败: {str(e)}"}, ensure_ascii=False)
        except Exception as e:
            logger.error(f"通知发送失败: {str(e)}")
            return json.dumps({
                "success": False,
                "error": str(e)
            }, ensure_ascii=False)
    
    async def _arun(self, *args, **kwargs):
        raise NotImplementedError()


